package com.sunday.pinxinghui.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.pinxinghui.R;

import com.sunday.pinxinghui.entity.Brand;

import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author by Damon,  on 2017/12/1.
 */

public class SelectBrandWindow extends PopupWindow {


    @Bind(R.id.grid_view)
    GridView gridView;

    private Context mContext;
    private int catId;
    private BrandAdapter adapter;
    private Click click;

    public SelectBrandWindow(Context context, int catId) {
        this.mContext = context;
        View windowView = LayoutInflater.from(mContext).inflate(R.layout.layout_select_brand, null);
        int appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        this.catId = catId;
        setContentView(windowView);
        ButterKnife.bind(this, windowView);
        this.setWidth(appWidth);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());

        //this.setAnimationStyle(R.style.popuwindow);
        adapter = new BrandAdapter();
        getBrands();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectIndex(position);
                adapter.notifyDataSetChanged();
                if (click != null) {
                    click.click(dataSet.get(position));
                    dismiss();
                }

            }
        });

    }

    private List<Brand> dataSet = new ArrayList<>();

    private void getBrands() {
        Call<ResultDO<List<Brand>>> call = ApiClient.getApiAdapter().getBrands(catId);
        call.enqueue(new Callback<ResultDO<List<Brand>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Brand>>> call, Response<ResultDO<List<Brand>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    Brand brand=new Brand();
                    brand.setName("全部");
                    brand.setId(0);
                    dataSet.add(brand);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Brand>>> call, Throwable t) {

            }
        });
    }

    public void setClick(Click click) {
        this.click = click;
    }

     class BrandAdapter extends BaseAdapter {

        private int selectIndex=-1;


        @Override
        public int getCount() {
            return dataSet.size();
        }

        @Override
        public Object getItem(int position) {
            return dataSet.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_brand_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Brand item = dataSet.get(position);
            if (position == selectIndex) {
                viewHolder.brandName.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            } else {
                viewHolder.brandName.setTextColor(ContextCompat.getColor(mContext, R.color._333));
            }
            viewHolder.brandName.setText(item.getName());
            return convertView;
        }

         void setSelectIndex(int selectIndex) {
            this.selectIndex = selectIndex;
        }


        class ViewHolder {
            @Bind(R.id.brand_name)
            TextView brandName;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }


    public interface Click {
        public void click(Brand brand);
    }

    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.7f);
        } else {
            this.dismiss();
        }
    }

    //显示
    public void showDropDown(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent);
            backgroundAlpha(0.7f);
        } else {
            this.dismiss();
        }
    }


    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
            dismiss();
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}